﻿using AForge.Video.DirectShow;
using AJWPFAdmin.Core.Mvvm;
using AJWPFAdmin.Core.Utils;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace AJWPFAdmin.Core.Components.AJFilePicker.ViewModels
{
    public class USBCameraPreviewerViewModel : ViewModelBase, IDialogAware
    {
        private string _dialogTitle;
        public string DialogTitle
        {
            get { return _dialogTitle; }
            set { SetProperty(ref _dialogTitle, value); }
        }

        private BitmapSource _image;
        public BitmapSource Image
        {
            get { return _image; }
            set { SetProperty(ref _image, value); }
        }

        private DelegateCommand _closeCmd;
        public DelegateCommand CloseCmd =>
            _closeCmd ?? (_closeCmd = new DelegateCommand(ExecuteCloseCmd));

        void ExecuteCloseCmd()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        private DelegateCommand _captureCmd;
        public DelegateCommand CaptureCmd =>
            _captureCmd ?? (_captureCmd = new DelegateCommand(ExecuteCaptureCmd));

        void ExecuteCaptureCmd()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, new DialogParameters
            {
                {"data", BitmapFrame.Create(Image)}
            }));
        }

        private System.Windows.Controls.Image _imgCtrl;
        private DelegateCommand<System.Windows.Controls.Image> _imageCtrlLoadedCmd;
        public DelegateCommand<System.Windows.Controls.Image> ImageCtrlLoadedCmd =>
            _imageCtrlLoadedCmd ?? (_imageCtrlLoadedCmd = new DelegateCommand<System.Windows.Controls.Image>(ExecuteImageCtrlLoadedCmd));

        void ExecuteImageCtrlLoadedCmd(System.Windows.Controls.Image parameter)
        {
            _imgCtrl = parameter;
        }

        public USBCameraPreviewerViewModel()
        {

        }

        public string Title => string.Empty;

        public event Action<IDialogResult> RequestClose;

        private VideoCaptureDevice _device;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            if (_device != null)
            {
                _device.NewFrame -= OnNewFrameHandle;
                _device.SignalToStop();
            }
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            parameters.TryGetValue<string>("data", out var monikerString);

            try
            {
                _device = new VideoCaptureDevice(monikerString);
                _device.NewFrame += OnNewFrameHandle;
                _device.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "摄像头启动失败", MessageBoxButton.OK);
            }

        }

        private void OnNewFrameHandle(object sender, AForge.Video.NewFrameEventArgs e)
        {
            _imgCtrl.Dispatcher.Invoke(() =>
            {
                Image = BitmapSourceConvert.ToBitmapSource(e.Frame);
            });
            
        }
    }
}
